<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetGoodsByCatid.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\goods;

use app\api\model\dao\Ad;
use app\api\model\dao\Goods;
use app\api\model\dao\GoodsCategory;
use app\library\Error;
use app\library\Common;
use app\library\PageBase;
use Think\Log;
use think\Validate;
use think\Cache;

class GetGoodsByCatid extends PageBase
{

    private $objGoods;
    private $objGoodsCategory;
    private $objAd;


    public function __construct()
    {
        parent::__construct();
        $this->objGoods = new Goods();
        $this->objGoodsCategory = new GoodsCategory();
        $this->objAd = new Ad();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cat_id' => 'require',
        ];
        $msg = [
            'cat_id.require' => '商品分类ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $cat_id = isset($param['cat_id']) ? intval($param['cat_id']) : 0;

        $goodsCategorys = array();
        $categorys = $this->objGoodsCategory->getAllGoodsCategory();
        Common::getCategoryTree($categorys, $cat_id, 10, $goodsCategorys);
        $subCategorys = array($cat_id);
        foreach ($goodsCategorys as $category) {
            $subCategorys[] = $category['cat_id'];
        }

        $arrField = array(
            'goods_id',
            'goods_name',
            'market_price',
            'shop_price',
            'goods_thumb',
            'sale_count',
        );
        $arrCond = array();
        $arrCond['cat_id'] = ['in', implode(',', $subCategorys)];
        if (!empty($param['is_hot'])) {
            $arrCond['is_hot'] = ['=', 1];
        }

        if (!empty($param['is_new'])) {
            $arrCond['is_new'] = ['=', 1];
        }

        if (isset($param['price_start']) && !empty($param['price_end'])) {
            $arrCond['shop_price'] = ['>=', $param['price_start']];
            $arrCond['shop_price '] = ['<=', $param['price_end']];
        }

        $orders = array(
            'dsc' => ' shop_price desc ',
            'asc' => 'shop_price asc ',
        );
        $orderby = ' create_time desc ';
        if (!empty($param['price_sort']) && !empty($orders[$param['price_sort']])) {
            $orderby = $orders[$param['price_sort']];
        }

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objGoods->getCount($arrCond);
        $rets = $this->objGoods->getGoodsByCond($arrCond, $arrField, $offset, $limit, $orderby);
        if (false === $rets) {
            return $this->getRes(Error::ERR_SYS);
        }

        foreach ($rets as &$ret) {
            $ret['discount'] = '' . round($ret['shop_price'] / $ret['market_price'], 2) * 10;
        }

        $now = time();
        // 获分类商品筛选中部广告
        $arrFields = [
            'ad_id',
            'ad_name',
            'ad_link',
            'ad_code',
            'ad_type',
            'ad_value',
        ];
        $arrConds = [
            'pid' => ['=', 3],
            'enabled' => ['=', 1],
            'status' => ['=', 0],
            //'start_time' => ['<=', $now],
            //'end_time' => ['>=', $now],
        ];
        $ads = $this->objAd->getAdByCond($arrConds, $arrFields);
        $ads = !empty($ads) ? $ads : array();

        $this->data = array(
            'list' => $rets,
            'total' => $counts,
            'ad' => $ads,
        );
        return $this->getRes();
    }
}
